
local Enemy = class("Enemy");

local speed = 1;
<<<<<<< HEAD
local bulletSpeed = 5;
=======
local bulletSpeed = 10;
>>>>>>> 6938802c5424161dcf6ba07ba781784b46679ef8

function Enemy:ctor(go)
	self.gameObject = go;
	self.transform = go.transform;
	self.firePoint = self.transform:Find("firepoint");
	self.hp = 50;
<<<<<<< HEAD
	self.lastFireTime = 0;

=======
	
>>>>>>> 6938802c5424161dcf6ba07ba781784b46679ef8
	local luaTrigger = self.gameObject:AddComponent(typeof(LuaTrigger2D));
	luaTrigger:SetExitFunc(self.OnTriggerExit, self);
	luaTrigger:SetEnterFunc(self.OnTriggerEnter, self);
	
	UpdateBeat:Add(self.Update, self);
end

function Enemy:Update()
	self.transform:Translate(Vector3.down * speed * Time.deltaTime);
	self:Fire();
<<<<<<< HEAD
end

function Enemy:Fire()
	if(Time.time - self.lastFireTime > 0.5) then
		self.lastFireTime = Time.time;
        
        --创建子弹，并发射
		local go = poolMgr:Get("EnemyBullet");
		go.transform.position = self.firePoint.position;
		go.transform.rotation = self.firePoint.rotation;
		
		local body = go:GetComponent('Rigidbody2D');
		body.velocity = self.firePoint.up * bulletSpeed;
	end
end

=======
end

local lastFireTime = 0;
function Enemy:Fire()
	if(Time.time - lastFireTime > 0.1) then
		lastFireTime = Time.time;
        
        --创建子弹，并发射
		local go = poolMgr:Get("EnemyBullet");
		go.transform.position = self.firePoint.position;
		go.transform.rotation = self.firePoint.rotation;
		
		local body = go:GetComponent('Rigidbody2D');
		body.velocity = self.firePoint.up * bulletSpeed;
	end
end

>>>>>>> 6938802c5424161dcf6ba07ba781784b46679ef8
--飞出边框，销毁自己
-- function Enemy.OnTriggerExit(self, collision)
function Enemy:OnTriggerExit(collision)
	if(collision:CompareTag('Destroy')) then
		self:Destroy();
	end
end

--碰到子弹掉血
function Enemy:OnTriggerEnter(collision)
	if(collision:CompareTag('PlayerBullet')) then
<<<<<<< HEAD
		poolMgr:Release("PlayerBullet",collision.gameObject);
		
		self.hp = self.hp - 1;
		if(self.hp <= 0) then
			self:Destroy();
			Event.Brocast("EnemyDead");
=======
		self.hp = self.hp - 1;
		if(self.hp <= 0) then
			self:Destroy();
>>>>>>> 6938802c5424161dcf6ba07ba781784b46679ef8
		end
	end
end

function Enemy:Destroy()
	print("Enemy:Destroy");
	UpdateBeat:Remove(self.Update, self);
	destroy(self.gameObject);
end

return Enemy; 